package com.dff.jrfw;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.databinding.DataBindingUtil;

import com.dff.jrfw.common.Constant;
import com.dff.jrfw.databinding.ActivityAccountLogoutBinding;
import com.dff.jrfw.model.LoginAccountRecordModel;
import com.dff.jrfw.remote.CustomCallBack;
import com.dff.jrfw.remote.RetrofitUtils;
import com.dff.jrfw.utils.GsonUtils;
import com.dff.jrfw.utils.SharedPreferenceUtils;
import com.dff.jrfw.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

public class AccountLogoutActivity extends BaseActivity implements View.OnClickListener {

    private ActivityAccountLogoutBinding binding;

    public static void callMe(Context context) {
        Intent intent = new Intent(context, AccountLogoutActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_account_logout);
        initView();
    }

    private void initView() {
        binding.ivBack.setOnClickListener(this);
        binding.btnLogOut.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_log_out:
//                initPop();
                doLogout();
                break;
        }
    }

    private void initPop() {
        View popView = LayoutInflater.from(AccountLogoutActivity.this).inflate(R.layout.pop_log_out_reason_layout, null);
        final PopupWindow popupWindow = new PopupWindow(popView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        popView.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
        popView.findViewById(R.id.btn_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();

            }
        });


        //设置SelectPicPopupWindow弹出窗体可点击
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(getResources().getColor(R.color.transparent_background_25));
        // 设置PickPopupWindow弹出窗体的背景
        popupWindow.setBackgroundDrawable(dw);
        popupWindow.setClippingEnabled(false);
        popupWindow.setAnimationStyle(R.style.PopupAnimaFade);
        popupWindow.showAtLocation(popView, Gravity.BOTTOM, 0, 0);
    }


    private void doLogout() {
        showLoading();
        RetrofitUtils.getService().doLogout("1", "1").enqueue(new CustomCallBack<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                hideLoading();
                try {
                    if (response.body() != null) {
                        String result = response.body().string();
                        JSONObject jsonObject = new JSONObject(result);
                        int code = jsonObject.getInt("code");
                        if (code == 200) {
                            String json = (String) SharedPreferenceUtils.getInstance().get(AccountLogoutActivity.this, Constant.LOGIN_ACCOUNT_RECORD, "");
                            LoginAccountRecordModel loginAccountRecordModel = GsonUtils.gsonToBean(json, LoginAccountRecordModel.class);
                            if (loginAccountRecordModel != null && loginAccountRecordModel.getLoginRecord() != null && loginAccountRecordModel.getLoginRecord().size() > 0) {
                                ArrayList<String> loginRecord = loginAccountRecordModel.getLoginRecord();
                                finish();
                                LoginTwoActivity.callMe(AccountLogoutActivity.this, loginRecord,true);
                            } else {
                                finish();
                                LoginActivity.callMe(AccountLogoutActivity.this, null,true);
                            }
                            finish();
//                            LoginActivity.callMe(AccountLogoutActivity.this, null);
                        } else {
                            String msg = jsonObject.getString("msg");
                            ToastUtil.toast(msg);
                        }
                    } else {
                        ResponseBody errorResponseBody = response.errorBody();
                        if (errorResponseBody != null) {
                            ToastUtil.toast(errorResponseBody.string());
                        }
                    }
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call call, Throwable t) {
                super.onFailure(call, t);
                hideLoading();
            }
        });
    }


}